import os

def clean_temp_files(folder_path):
    try:
        # 遍历指定文件夹下的所有文件和子文件夹
        for root, dirs, files in os.walk(folder_path):
            for file in files:
                # 检查文件扩展名是否为 .tmp
                if file.endswith('.tmp'):
                    file_path = os.path.join(root, file)
                    try:
                        # 删除临时文件
                        os.remove(file_path)
                        print(f"已删除临时文件: {file_path}")
                    except Exception as e:
                        print(f"删除文件 {file_path} 时出错: {e}")
    except Exception as e:
        print(f"遍历文件夹 {folder_path} 时出错: {e}")

# 指定要清理的文件夹路径
folder_to_clean = 'C:\\Temp'
clean_temp_files(folder_to_clean)